package de.jmda.sample.gen.helloworld;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.junit.Test;

import de.jmda.gen.GeneratorException;
import de.jmda.gen.java.ClassGenerator;
import de.jmda.gen.java.CompilationUnitUtil;
import de.jmda.gen.java.impl.DefaultClassGenerator;

public class JUTHelloWorld
{
	@Test
	public void testDefault() throws GeneratorException
	{
		ClassGenerator generator = new DefaultClassGenerator();
		System.out.println(generator.generate());
	}

	@Test
	public void testModifiedName() throws GeneratorException
	{
		ClassGenerator generator = new DefaultClassGenerator();
		generator.setClassName("HelloWorld");
		System.out.println(generator.generate());
	}

	@Test
	public void testAddedMainMethod() throws GeneratorException
	{
		ClassGenerator generator = new DefaultClassGenerator();
		generator.setClassName("HelloWorld");
		generator.setMainMethod("System.out.println(\"hello jmda.gen\");");
		System.out.println(generator.generate());
	}

	@Test
	public void testWriteToFile() throws GeneratorException, IOException
	{
		ClassGenerator generator = new DefaultClassGenerator();
		
		String packageName = "de.jmda.gen.java.sample.helloworld";
		String className = "HelloWorld";
		
		generator.setPackageName(packageName);
		generator.setClassName(className);
		generator.setMainMethod("System.out.println(\"hello jmda.gen\");");

//		GeneratorValidation.validate(generator);

		System.out.println(generator.generate());
		
		File compilationUnit =
				CompilationUnitUtil.buildFile(
						"src/gen/java", packageName, className);
		
		FileUtils.writeStringToFile(compilationUnit, generator.generate().toString());
	}
}